package com.xczl.book.test.service;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.shishuo.cms.entity.vo.PageVo;
import com.xczl.book.entity.Book;
import com.xczl.book.service.BookService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "classpath:applicationContext.xml" })
public class BookServiceTestCase {

	@Autowired
	private BookService bookService;

	/**
	 * 添加图书
	 */
	@Test
	@Transactional
	public void addBook() {
		Book book = createBook();
		bookService.add(book);
		Book temp = bookService.findById(book.getBookId());
		Assert.notNull(temp);
	}

	/**
	 * 创建图书
	 * 
	 * @return
	 */
	private Book createBook() {
		Book book = new Book();
		book.setCateId(1);
		book.setNumber("978-7-8623-4357-8");
		book.setUrlPic("upload/blank.jpg");
		book.setName("嫌疑人X的献身");
		book.setAuthor("东野圭吾");
		book.setBrief("《嫌疑人X的献身》内容简介：《嫌疑人X的献身》是日本天王作家东野圭吾“迄今为止很高杰作”，创造了日本推理小说史上绝无仅有的奇迹，将第134届直木奖、第6届本格推理小说大奖及日本3大推理小说排行榜年度总冠军一并收入囊中。");
		book.setPress("南海出版公司");
		book.setTotalQuantity(200);
		return book;
	}

	/**
	 * 修改商品
	 */
	@Test
	@Transactional
	public void update() {
		// 注册
		Book book = createBook();
		bookService.add(book);
		// 修改商品
		book.setNumber("978-7-8623-4372-1");
		bookService.update(book);
		// 获取更新信息
		Book book1 = bookService.findById(book.getBookId());
		// 验证结果
		Assert.isTrue(StringUtils.equals(book.getNumber(), book1.getNumber()));
	}

	/**
	 * 查询
	 */
	@Test
	@Transactional
	public void find() {
		Book searchVo = new Book();
		searchVo.setNumber("978");
		searchVo.setAuthor("你还");
		PageVo<Book> page = bookService.findPageVo(1, searchVo);
		for (Book book : page.getList()) {
			System.out.println(book.toString());
		}
	}
	
	/**
	 * 根据编号查询
	 */
	@Test
	@Transactional
	public void findByNumber() {
		Book book=bookService.findByNumber("978-7-5623-4372-1");
		System.out.println(book.getName());
	}
	
	/**
	 * 修改数量
	 */
	@Test
	@Transactional
	public void editQuantity(){
		// 注册
		Book book = createBook();
		bookService.add(book);
		//调用修改数量的方法
		bookService.editQuantity(book.getBookId(),100);
		//bookId,quantity
		Book temp = bookService.findById(book.getBookId());
		//验证 通过ID查 temp
		int fuceQuantity=book.getTotalQuantity()+100;
		Assert.isTrue(fuceQuantity==temp.getTotalQuantity());
		//比对temp总数量是否等于创建的数量加
	}
}
